package com.example.bilibili.service;

import com.example.bilibili.domain.auth.*;
import com.example.bilibili.domain.constant.AuthRoleConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class UserAuthService {

    @Autowired
    private UserRoleService userRoleService;

    @Autowired
    private AuthRoleService authRoleService;

    public UserAuthorities getUserAuthorities(Long userId) {
        // 查询用户角色集合 roleIdSet
        List<UserRole> userRoleList = userRoleService.getUserRoleByUserId(userId);
        Set<Long> roleIdSet = userRoleList.stream().map(UserRole::getRoleId).collect(Collectors.toSet());

        // 查询按钮操作权限
        List<AuthRoleElementOperation> roleElementOperationList = authRoleService.getRoleElementOperationsByRoleIds(roleIdSet);

        // 查询页面菜单权限
        List<AuthRoleMenu> authRoleMenuList = authRoleService.getAuthRoleMenusByRoleIds(roleIdSet);

        // 封装用户的所有权限
        UserAuthorities userAuthorities = new UserAuthorities();
        userAuthorities.setRoleElementOperationList(roleElementOperationList);
        userAuthorities.setRoleMenuList(authRoleMenuList);
        return userAuthorities;
    }

    // 添加新用户的默认权限角色
    public void addUserDefaultRole(Long id) {
        // 创建 (用户) -> (角色) 实体类
        UserRole userRole = new UserRole();

        // 通过默认角色 code 查询 默认角色的信息：id
        AuthRole role = authRoleService.getRoleByCode(AuthRoleConstant.ROLE_LV0);

        // 添加用户的默认权限角色
        userRole.setUserId(id);
        userRole.setRoleId(role.getId());
        userRole.setCreateTime(new Date());
        userRoleService.addUserRole(userRole);
    }
}
